package verins.GUI;

import verins.beans.Rol;
import verins.beans.Permiso;
import verins.BO.PermisoBO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import verins.BO.RolBO;
import verins.Utils.Comunes;
import verins.Utils.Mensajes;
import verins.beans.Permisoxrol;
import verins.beans.PermisoxrolPK;

public class frmModificacionPermisos extends javax.swing.JFrame {

    List<Permiso> todos;
    Rol rol;

    public frmModificacionPermisos(Rol rol) {
        initComponents();
        Comunes.centrarVentana(this);
        txtRol.setEnabled(false);
        this.rol=rol;
        txtRol.setText(rol.getDescripcion());
        todos= PermisoBO.findAll();
        verins.Utils.Comunes.limpiarTabla(tblPermisos);
        DefaultTableModel dtm=(DefaultTableModel)tblPermisos.getModel();
        for (Permiso p : todos){
            Object[] fila= new Object[]{
                p.getDescripcion(),
                Boolean.FALSE
            };
            dtm.addRow(fila);
        }
        List<Permisoxrol> permisosRol = rol.getPermisoxrolList();
        for (Permisoxrol p : permisosRol){
            for (int i=0; i<dtm.getRowCount(); i++){
                if (dtm.getValueAt(i, 0).equals(p.getPermiso().getDescripcion())){
                    dtm.setValueAt(Boolean.TRUE, i, 1);
                }
            }
        }

    }

    public frmModificacionPermisos() {
        initComponents();
        Comunes.centrarVentana(this);
        this.rol=null;
        todos= PermisoBO.findAll();
        verins.Utils.Comunes.limpiarTabla(tblPermisos);
        DefaultTableModel dtm=(DefaultTableModel)tblPermisos.getModel();
        for (Permiso p : todos){
            Object[] fila= new Object[]{
                p.getDescripcion(),
                Boolean.FALSE
            };
            dtm.addRow(fila);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblRol = new javax.swing.JLabel();
        txtRol = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPermisos = new javax.swing.JTable();
        btnSalir = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modificación de Permisos");

        lblRol.setText("Rol");

        txtRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRolActionPerformed(evt);
            }
        });

        tblPermisos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nombre del Permiso", "Tiene Permiso"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblPermisos.getTableHeader().setResizingAllowed(false);
        tblPermisos.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblPermisos);

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(72, Short.MAX_VALUE)
                .addComponent(lblRol)
                .addGap(53, 53, 53)
                .addComponent(txtRol, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(159, 159, 159))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(346, 346, 346)
                        .addComponent(btnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRol)
                    .addComponent(txtRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnAceptar))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void txtRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRolActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtRolActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        
        if (verins.Utils.Mensajes.guardar(evt, this)){
            if (txtRol.getText().trim().length()>0){
                if (rol==null){
                    rol=new Rol();
                    rol.setDescripcion(txtRol.getText());
                    boolean exito=RolBO.save(rol);
                    if (!exito){
                        verins.Utils.Mensajes.exito(exito);
                        return;
                    }
                }
                }else {
                    Mensajes.alerta("Debe colocar un nombre de rol");
                    return;
                }

                rol.getPermisoxrolList().clear();

                DefaultTableModel dtm=(DefaultTableModel)tblPermisos.getModel();
                for (Permiso p : todos){
                    Boolean esta=false;
                    for (int i=0; i<dtm.getRowCount(); i++){
                        if (dtm.getValueAt(i, 0).equals(p.getDescripcion())){
                            esta = (Boolean)dtm.getValueAt(i, 1);
                            break;
                        }
                    }
                    if (esta){
                        Permisoxrol pxr= new Permisoxrol();
                        pxr.setRol(rol);
                        pxr.setPermiso(p);
                        PermisoxrolPK pxrpk= new PermisoxrolPK();
                        pxrpk.setIdpermiso(p.getIdpermiso());
                        pxrpk.setIdrol(rol.getIdrol());
                        pxr.setPermisoxrolPK(pxrpk);
                        rol.getPermisoxrolList().add(pxr);
                    }
                }


                if(verins.Utils.Mensajes.exito(RolBO.save(rol)))
                    this.dispose();
            }


    }//GEN-LAST:event_btnAceptarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblRol;
    private javax.swing.JTable tblPermisos;
    private javax.swing.JTextField txtRol;
    // End of variables declaration//GEN-END:variables

}
